// 图片列表
controllers.controller('imageListCtrl', ['$scope', '$http', '$rootScope', '$modal', '$window', 'Image', 'AlertService', 'Upload',
    function ($scope, $http, $rootScope, $modal, $window, Image, AlertService, Upload) {

        $scope.resetCurrentPageName("商品维护");
        $scope.utils = Food.utils;
        $scope.pageSize = 20;
        $scope.currentPage = 1;
        $scope.imageList = Image.imageList = [];
        $rootScope.selectedItemList = [];

        $scope.queryList = function () {
            Image.queryList($scope.uploadTime, $scope.currentPage, $scope.pageSize, function () {
            });
        };
        $scope.queryList();

        $scope.innerOk = function () {
            //获取已选择的列表
            $rootScope.selectedItemList = [];
            for (var entity in $scope.imageList) {
                if ($scope.imageList[entity].selected) {
                    $rootScope.selectedItemList.push($scope.imageList[entity]);
                }
            }
            $rootScope.ok();
        };

        $scope.removeSelect = function () {
            //获取已选择的列表
            $rootScope.selectedItemList = [];
            for (var entity in $scope.imageList) {
                if ($scope.imageList[entity].selected) {
                    $rootScope.selectedItemList.push($scope.imageList[entity].url);
                }
            }
            Image.removeImageList($rootScope.selectedItemList, function () {
                AlertService.addAlert({type: 'success', msg: '图片删除完成'}, 1000);
                $window.location.reload();
            });
        }

        $scope.$watch('files', function () {
            $scope.upload($scope.files);
        });
        $scope.log = '';
        $scope.isRunning = false;
        $scope.upload = function (files) {
            if (files && files.length) {
                $scope.isRunning = true;
                for (var i = 0; i < files.length; i++) {
                    var file = files[i];
                    Upload.upload({
                        url: '/azeroth/back/image/uploadItemFile.htm',
                        fields: {
                            'actionType': "initItemTag"
                        },
                        file: file
                    }).progress(function (evt) {

                    }).success(function (data, status, headers, config) {
                        AlertService.addAlert({type: 'success', msg: '图片正在完成'}, 1000);
                        Image.imageList.push({url: data.data});
                        $scope.isRunning = false;
                    });
                }
            }
        };


    }]);

